<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Article_model extends OC_Model {

	public function get_items($limit,$offset,$cat_id)
	{
		$this->db->select('a.*,b.ca_name,b.ca_id');
		$this->db->from('articles a');
		$this->db->join('category_article b', 'b.ca_id = a.ca_id', 'left');
		if(!empty($cat_id)) $this->db->where('a.ca_id', $cat_id);
		if(!empty($limit)) $this->db->limit($limit,$offset);
		return $this->db->get()->result();
	}


    public function count($limit=false,$offset=false,$cat_id=false)
    {
        return count($this->get_items($limit,$offset,$cat_id));
    }


	public function get_categories()
	{
		$this->db->order_by('ca_order', 'Asc');
		$this->db->where('ca_active', 1);
		return $this->db->get('category_article')->result();
	}


	public function get_item_details($id=false){
		$this->db->select('a.*,b.ca_name,b.ca_id');
		$this->db->from('articles a');
		$this->db->join('category_article b', 'b.ca_id = a.ca_id', 'left');
		$this->db->where('a.a_id', $id);
		return $this->db->get()->row_array();
	}

	public function item_per_category($cat,$limit,$offset)
	{
		$this->db->select('a.*,b.ca_name,b.ca_id');
		$this->db->from('articles a');
		$this->db->join('category_article b', 'b.ca_id = a.ca_id', 'left');
		$this->db->where('a.ca_id', $cat);
		if(!empty($limit)) $this->db->limit($limit,$offset);
		return $this->db->get()->result();
	}

	public function get_meta($meta=false,$id=false)
	{
		$this->db->select('a_meta_'.$meta);
		$this->db->where('a_id', $id);
		$meta_data = $this->db->get('articles')->row_array();
		return $meta_data['a_meta_'.$meta];
	}

	public function do_comment($object)
	{
		return $this->db->insert('comment_article', $object);
	}

	public function get_comments($id=false)
	{
		$this->db->where('a_id', $id);
		$this->db->where('ca_approved', 1);
		return $this->db->get('comment_article')->result();
	}
	

}

/* End of file article_model.php */
/* Location: ./applications/module_front/articles/models/article_model.php */